package com.elite.groovy.callback;

import groovy.json.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.RequestDispatcher;
import com.elite.groovy.util.EliteBean;
import cn.com.elite.PropertiesUtil;
import java.security.MessageDigest;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;

import net.sf.json.JSONObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.wisdge.dataservice.Result;
import com.elite.groovy.util.Util;
import cn.com.kh.HttpClientProxy;
import org.apache.commons.lang.StringEscapeUtils;
/**
 * 
 * @author chopper
 * @use {"requestType": "login","passWord": "111111","agentId": "3001","staffId": "AELITE","dn": "sip:1003579"} 登录接口
 * @use {"requestType": "makeCall","agentId": "25803364","staffId": "AELITE","dnis": "13212345678","ani":"1234","userField": ""} 拨打接口
 *
 */

public class QnxlInterface implements ServiceBackInvoke{
	private static final Log log=LogFactory.getLog(QnxlInterface.class);

	public String doCallBack(EliteBean eliteBean,HttpServletRequest request,HttpServletResponse response) {
		response.setCharacterEncoding("UTF-8");
		JSONObject result = new JSONObject();
		log.debug("apiInterfaceTest start-----------------");
		def requestMsg = Util.getRequestBody(request,'utf-8');
		def requestJson = JSONObject.fromObject(requestMsg);
		log.debug("apiInterfaceTest request----------------:"+requestMsg);
		def requestType = requestJson.getAt("requestType");
		def infoMsg =  qnxlCallGetInfo(requestJson.getAt("staffId"));
		def infoJson = JSONObject.fromObject(infoMsg);
		JSONObject callJson = new JSONObject();
		callJson.put("agentId", requestJson.getAt("agentId"));
		callJson.put("entID", infoJson.getAt("entId"));
		callJson.put("requestId", requestJson.getAt("staffId"));
		callJson.put("mac", infoJson.getAt("macString"));
		callJson.put("timestamp", infoJson.getAt("timeStamp"));
		if(requestType.equals("login")) {
			callJson.put("passWord", "111111");
			callJson.put("dn", requestJson.getAt("dn"));
		}else if(requestType.equals("makeCall")) {
			callJson.put("number", requestJson.getAt("dnis"));
			callJson.put("userData", Util.getGuid().replaceAll("-",""));
			callJson.put("userField", "");
			callJson.put("dpsEventUrl", "http://localhost:8888/agent/dps/receiveDpsEvent");
			callJson.put("dpsDetailUrl", "http://localhost:8888/agent/dps/receiveDpsDetail");
			callJson.put("ani", requestJson.getAt("ani"));
		}else{
			result.put("code", -1);
			result.put("msg", "获取操作类型失败！");
			return result
		};
		return qnxlProxy(requestType,callJson.toString());
	}
	
/**
 * 
 * @use	获取青牛续联接口相关信息
 * @param requestId
 * @return
 * @throws Exception
 */
	
	private String qnxlCallGetInfo(String requestId) throws Exception {
		JSONObject result = new JSONObject();
		PropertiesUtil cfg = new PropertiesUtil();
		try {
			String entId = cfg.readValue("QingNiuXL.EntId");
			String entSecret = cfg.readValue("QingNiuXL.SECRET_KEY");
			String timeStamp = Util.getNowString("yyyyMMddHHmmss");
			String macString = Util.getMD5Str(entId + entSecret + timeStamp + requestId).toUpperCase();
			result.put("state", 1);
			result.put("entId", entId);
			result.put("timeStamp", timeStamp);
			result.put("macString", macString);
			return result;
		}catch(Exception e) {
			log.error("ErrorMessage:",e);
			result.put("state", 0);
			result.put("entId", "");
			result.put("timeStamp", "");
			result.put("macString", "");
			return result;
			
		}
	}
	
	/**
	 * 
	 * @use 青牛续联接口
	 * @param callJson
	 * @return
	 * @throws Exception
	 */
	
	private String qnxlProxy(String requestType,String callJson) throws Exception {
		JSONObject result = new JSONObject();
		PropertiesUtil cfg = new PropertiesUtil();
		String ccodUrl = cfg.readValue("QingNiuXL.CCODURL");
		String proxyIp = cfg.readValue("QingNiu.ProxyIP");
		int proxyPort = cfg.readValue("QingNiu.ProxyPort").toInteger();
		try {
			def reJson = HttpClientProxy.sendPost(ccodUrl+"/agent/user/"+requestType,callJson,proxyIp,proxyPort);
			log.debug("apiInterfaceTest end-----------------");
			if(reJson.length()<=0 || reJson==null){
				result.put("code", -1);
				result.put("msg", "转发服务请求异常！");
				return result
			 }else {
				 return reJson
			 }		 
		}catch(Exception e) {
			log.error("ErrorMessage:",e);
			result.put("code", -1);
			result.put("msg", "转发服务请求异常！");
			return result		
		}
	}
	
}